﻿using OnlineBookstore.HelpClass;
using OnlineBookstore.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineBookstore.Controllers
{
	public class ShopInfoController : Controller
	{
		BookcityEntities db = new BookcityEntities();

		public ActionResult GetShopList()
		{
			var data = db.shopInfo;
			return View(data.ToList());
		}


		public ActionResult Index()
		{
			return View();
		}
		/// <summary>
		/// 登录
		/// </summary>
		/// <param name="shop"></param>
		/// <returns></returns>
		[HttpPost]
		public ActionResult Login(shopInfo shop)
		{
			var data = db.shopInfo.Where(m => m.ShopLogin == shop.ShopLogin && m.ShopPassword == shop.ShopPassword).FirstOrDefault();


			if (data != null)
			{
				//var x = db.CapitalAdvance.Where(m => m.Id == data.Id).FirstOrDefault();
				Session["Id"] = data.Id;
				Session["ShopName"] = data.ShopName;

				return Redirect("/BookDetailsInfo/Index");
			}
            var data2 = db.shopInfo.Where(m => m.ShopLogin == shop.ShopLogin).FirstOrDefault();

            if (data2 ==null)
            {
                ViewBag.LoginError = "用户名错误";
            }
			else
            {
                ViewBag.PasswordError = "密码错误";
            }
            return View();
		}
		[HttpGet]
		public ActionResult Login()
		{
			return View();

		}
		/// <summary>
		/// 账号客户信息
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public ActionResult AddRegister(ShopInfoParam shop)
		{
			var shops = shop.MapTo<shopInfo, ShopInfoParam>();

			Inspect(shops);

			if (ModelState.IsValid)
			{
				;
				db.shopInfo.Add(shops);
				db.SaveChanges();//保存
				return RedirectToAction("Login");
			}
			return View();
		}
		public ActionResult AddRegister()
		{
			return View();
		}

		/// <summary>
		/// 检查字符串格式
		/// </summary>
		/// <param name="shop"></param>
		public void Inspect(shopInfo shop)
		{
			if (shop.ShopLogin == null)
			{
				throw new NullReferenceException("账号不能为空");
			}
			if (shop.ShopName == null)
			{
				throw new Exception("姓名不能为空");
			}
			if (shop.ShopPhone == null)
			{
				throw new Exception("电话不能为空");
			}
			if (shop.ShopRegio == null)
			{
				throw new NullReferenceException("请选择地址");
			}
			if (shop.ShopAddress == null)
			{
				throw new NullReferenceException("请输入详细地址");
			}
			shop.ShopDate = DateTime.Now;
			shop.ShopCode = "CMD-" + DateTime.Now.ToString("yyyyMMddHHmmss");
		}



		/// <summary>
		/// 后台添加
		/// </summary>
		/// <param name="shop"></param>
		/// <returns></returns>
		[HttpPost]

		public ActionResult AddShopInfo(shopInfo shop)
		{
			if (shop == null)
			{
				throw new NullReferenceException("请输入信息");
			}
			Inspect(shop);//检查
			if (ModelState.IsValid)
			{
				db.shopInfo.Add(shop);
				db.SaveChanges();
				return RedirectToAction("GetShopList");
			}

			return View();
		}
		[HttpGet]
		public ActionResult AddShopInfo()
		{
			return View();
		}

		/// <summary>
		/// 获取详情
		/// </summary>
		/// <param name="Id"></param>
		/// <returns></returns>
		public ActionResult UpdateShop(int? Id)
		{
			if (Id <= 0)
			{
				throw new NullReferenceException("Id不能为空");
			}
			shopInfo shop = db.shopInfo.Find(Id);
			if (shop == null)
			{
				return HttpNotFound();
			}
			return View(shop);
		}
		/// <summary>
		/// 修改客户信息
		/// </summary>
		/// <param name="shop"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult UpdateShop(shopInfo shop)
		{

			if (ModelState.IsValid)
			{

				db.Entry(shop).State = EntityState.Modified;
				db.SaveChanges();
				return RedirectToAction("GetShopList");
			}
			return View(shop);
		}


		public ActionResult DelShop(int? Id)
		{
			if (Id <= 0)
			{
				throw new NullReferenceException("Id不能为空");
			}
			shopInfo shop = db.shopInfo.Find(Id);
			if (shop == null)
			{
				return HttpNotFound();
			}
			return View(shop);
		}
		[HttpPost, ActionName("DelShop")]
		[ValidateAntiForgeryToken]
		public ActionResult DelShop(int Id)
		{

			shopInfo shop = db.shopInfo.Find(Id);
			db.shopInfo.Remove(shop);
			db.SaveChanges();
			return RedirectToAction("GetShopList");
		}

		
	}
}